"""
目的 :实时采集B站直播间弹幕信息 保存到 mongodb数据库
流程
1.抓到能获取直播间弹幕的接口   url
    https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=26481217


2.请求数据
3.清洗数据
4.保存到mongodb   评论人  内容
案例需要掌握  实时获取直播数据以及mongodb数据插入!

"""

# 1.抓到能获取直播间弹幕的接口   url
#     https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=26481217
import re
import time
import requests
import pymongo

def crawl_data():
    conn = pymongo.MongoClient(host="localhost", port=27017)
    db = conn["bili_datas"]
    colletion = db["bdata"]
    # 2.请求数据
    url = "https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=26481217"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
        "referer": "https://live.bilibili.com/26481217?hotRank=0&session_id=d069d5f3bf4c100ffe99b1f4426b2c1f_5A5DDB60-32CB-492D-94B3-03C0EA1FBEE7&visit_id=1dfyeaqwbhj4",
        "origin": "https://live.bilibili.com",
        "cookie": "buvid3=A9EB9286-B1A2-F5E0-FAC4-FA40F552665313087infoc; b_nut=1668859013; i-wanna-go-back=-1; _uuid=5A1118F1-96CF-74E7-7BB9-510AF8444210B614781infoc; nostalgia_conf=-1; CURRENT_FNVAL=4048; rpdid=|(u)l~lmJ~R~0J'uYY))lm|RJ; fingerprint=2ec0f0a772e5772924f6b52d2d5b1b3e; buvid_fp_plain=undefined; DedeUserID=663816254; DedeUserID__ckMd5=e33644a7ec719423; b_ut=5; CURRENT_QUALITY=116; blackside_state=0; LIVE_BUVID=AUTO7916691182122005; buvid4=3182C943-94E1-E33E-6F74-74B43B04052160109-022030913-ZDn%2BYby5P8CiJMeEDas4Sg%3D%3D; buvid_fp=2ec0f0a772e5772924f6b52d2d5b1b3e; hit-new-style-dyn=0; hit-dyn-v2=1; bp_video_offset_663816254=732348845344686100; SESSDATA=a3d19962%2C1684916446%2C34bd1%2Ab2; bili_jct=c87c422965438a1987dc1b0b22a2d887; sid=6ot6g9if; innersign=0; b_lsid=F1FE3276_184AED40C0C; bsource=search_baidu; _dfcaptcha=4cd28e6c682f7c28a55e3f315c7c27b2; PVID=20"
    }
    response = requests.get(url=url, headers=headers)
    res_data = response.content.decode()
    # print(res_data)
    # 3.清洗数据
    content_data = re.findall('\{"text":"(.*?)","dm_type":0,"uid":\d*,"nickname":"(.*?)","uname_color', res_data)
    print(content_data)
    for content in content_data:

        content = list(content)
        print(content)
        print(len(content))
        # 判断 真实的数据 是否是一个由杂乱字符的数据!  如果有这个正则则会捕获   并且进行数据的匹配
        # contents = re.findall('(.*?)"', content[0])
        # if content != []:
        #     content[0] = contents
        colletion.update_many({"name": content[-1], "text": content[0]},
                              {"$set": {"name": content[-1], "text": content[0]}}, upsert=True)


# 定时器函数 通过小时 分钟 秒 返回具体的秒数！
def set_time(hour, min, sec):
    return hour * 3600 + min * 60 + sec


if __name__ == '__main__':
    # crawl_data()
    # 每五秒执行一次
    result_time = set_time(0, 0, 5)
    print(result_time)
    while True:
        crawl_data()
        time.sleep(result_time)

# 2.请求数据
# 3.清洗数据
# 4.保存到mongodb   评论人  内容
